import Http from '../libs/Http.js';

var api = {};

var domain = '';
// 构造器
function createInstance(Vue, options) {
  if (api.http) {
    return api;
  }
  api.options = options;
  domain = options.domain;
  
  var http = new Http({
    dataType: 'json',
    responseType: 'text',
    ...options,
    header: options?.header || {},
    interceptor: options?.interceptor || {}
  });

  api.http = http;
  (options.modules || options.list || []).forEach(item => {
    let name, func, options;
    if (typeof item == 'string') {
      name = item;
      try {
        func = require('@/common/apis/' + name);
      } catch (e) {
        func = require('../apis/' + name);
      }
    } else if (Array.isArray(item)) {
      name = item[0];
      func = item[1] || name;
    } else if (typeof item == 'object') {
      name = item?.name;
      func = item?.require || require(item?.file);
      options = item?.options || {}
    }
    api[name] = func(http, api);
  })
  return api;
}

export {
  createInstance,
  domain
}

export default api
